package krillr.Predictors;
import robocode.*;
import robocode.util.Utils;
import krillr.Util.*;
import java.util.*;

public class GuessFactorC extends Predictor {
    static final int  SEARCH_DEPTH = 30;
    static final int BULLET_SPEED = 11;
    static final int MAX_RANGE = 800;
    static final int SEARCH_END_BUFFER = SEARCH_DEPTH + MAX_RANGE / BULLET_SPEED;
    
    double targetBearing = e.getBearingRadians() + getHeadingRadians();
    double ArcMovement = e.getVelocity() * Math.sin(e.getHeadingRadians() - targetBearing);
    int matchIndex;
    public GuessFactorC(KrillBot b, Tracker u) {
        bot = b;
        unit = u;
    }
    
    public void run() {
        Iterator it = new ArrayList(waves).iterator();
        while(it.hasNext()) {
            Wave w = (Wave)it.next();
            w.test();
        }
    }
    
    public void update() {
        arcLength[++historyIndex] = arcLength[historyIndex - 1] + ArcMovement;
        patternMatcher.append((char)(ArcMovement));
        do 
        {
            matchIndex = patternMatcher.lastIndexOf(patternMatcher.substring(Math.max(historyIndex-searchDepth,0)),historyIndex-SEARCH_END_BUFFER);
        } while (--searchDepth*matchIndex < -1); 
        matchIndex += searchDepth;
    }
    
    public Vector2d getVector() {
        Vector2d newVector = new Vector2d();
        double newBearing = Math.sin((arcLength[matchIndex+((int)(e.getDistance()/BULLET_SPEED))]-arcLength[matchIndex])/e.getDistance() + targetBearing);
        newVector.setDirection(newBearing);
        return newVector;
    }
    
    public double[][][][][] getSaveData() { return _gunStats; }
    
    public Wave getNewestWave() { return (Wave)waves.get(waves.size()-1); } 
}